var t = require("../../../@babel/runtime/helpers/interopRequireDefault"), e = require("../../../@babel/runtime/helpers/defineProperty"), a = t(require("../../../@babel/runtime/regenerator")), r = require("../../../@babel/runtime/helpers/asyncToGenerator"), s = require("../../../poster/poster.js"), i = getApp();

Component({
    behaviors: [ s ],
    properties: {
        goodsId: {
            type: null,
            optionalTypes: [ String, Number ]
        },
        ispt: {
            type: null
        },
        activityid: {
            type: null
        },
        store_id: {
            type: null
        },
        posterUrl: {
            type: String,
            value: "goods.goods-poster.new-goods-poster"
        }
    },
    data: {
        listData: []
    },
    ready: function() {},
    observers: {
        isshow: function(t) {
            console.log("eeeeeeeeeeeee", t, !this.data.listData[this.data.goodsId], this.data.getFlag), 
            t && !this.data.listData[this.data.goodsId] && this.data.getFlag ? (this.data.getFlag = !1, 
            this.setData({
                poster: "",
                loading: 0
            }), this.getPosterPic()) : "" != this.data.listData[this.data.goodsId] && this.setData({
                poster: this.data.listData[this.data.goodsId]
            });
        }
    },
    methods: {
        ptPoster: function() {
            if ("group_goods" == this.data.ispt || "zhpGroup" == this.data.ispt || "groupwork" == this.data.ispt || "ywmGroup" == this.data.ispt) {
                var t, e = this;
                "group_goods" == this.data.ispt ? (t = i.getNetAddresss("plugin.fight-groups.frontend.controllers.group-poster.generate-goods-poster"), 
                this.properties.store_id && (t += "&store_id=" + this.properties.store_id)) : "zhpGroup" == this.data.ispt ? t = i.getNetAddresss("plugin.zhp-group-lottery.frontend.activity-poster.generate-goods-poster") : "groupwork" == this.data.ispt ? t = i.getNetAddresss("plugin.group-work.frontend.controllers.group-poster.generate-goods-poster") : "ywmGroup" == this.data.ispt && (t = i.getNetAddresss("plugin.ywm-fight-groups.frontend.controllers.group-poster.generate-goods-poster")), 
                t += "&id=" + this.data.activityid, i._getNetWork({
                    url: t,
                    success: function(t) {
                        var a = t.data;
                        1 == a.result && e.setData({
                            poster: a.data.image_url
                        }), wx.hideLoading();
                    }
                }), setTimeout(function() {
                    wx.hideLoading();
                }, 5e3);
            } else {
                var a = this, r = i.getNetAddresss("plugin.snatch-regiment.api.GoodsPoster.generateGoodsPoster");
                r += "&id=" + this.data.goodsId, i._getNetWork({
                    url: r,
                    success: function(t) {
                        var e = t.data;
                        1 == e.result && a.setData({
                            poster: e.data.image_url
                        }), wx.hideLoading();
                    }
                }), setTimeout(function() {
                    wx.hideLoading();
                }, 5e3);
            }
        },
        getPosterPic: function() {
            var t = this;
            return r(a.default.mark(function e() {
                var r, s;
                return a.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        if (!t.data.ispt) {
                            e.next = 3;
                            break;
                        }
                        return t.ptPoster(), e.abrupt("return");

                      case 3:
                        return e.next = 5, t.getMyCanvasMessage(640, 1008);

                      case 5:
                        r = e.sent, t.setData({
                            canvasWidth: r.width,
                            canvasHeight: r.height
                        }), s = i.getNetAddresss(t.properties.posterUrl), i._getNetWork({
                            showToastIn: !1,
                            url: s,
                            data: {
                                id: t.data.goodsId
                            },
                            success: function(e) {
                                var a = e.data;
                                1 == a.result ? a.data.new && 1 == a.data.new ? t.startDrawCanvas(r, a.data) : t.startOldDrawCanvas(r, a.data) : wx.showToast({
                                    title: a.msg
                                });
                            },
                            complete: function() {
                                t.data.getFlag = !0;
                            }
                        });

                      case 9:
                      case "end":
                        return e.stop();
                    }
                }, e);
            }))();
        },
        startDrawCanvas: function(t, s) {
            var i = this;
            return r(a.default.mark(function o() {
                var n, d, c, g, l, p, h, u, w, f;
                return a.default.wrap(function(o) {
                    for (;;) switch (o.prev = o.next) {
                      case 0:
                        n = "原价", d = "现价";
                        try {
                            wx.getStorage({
                                key: "yz_basic_info",
                                success: function(t) {
                                    "" != t.data.lang.goods.market_price && (n = t.data.lang.goods.market_price), "" != t.data.lang.goods.price && (d = t.data.lang.goods.price);
                                }
                            });
                        } catch (t) {
                            console.log(t);
                        }
                        if (console.log("haobao"), (c = wx.createCanvasContext("myCanvas", i)).save(), i.drawRoundRectPath(c, t.width, t.height, 30), 
                        c.setFillStyle("#fff"), c.fillRect(0, 0, t.width, t.height), "background" != s.background.type) {
                            o.next = 17;
                            break;
                        }
                        return o.next = 12, i.downImage({
                            src: s.background.src
                        });

                      case 12:
                        g = o.sent, i.setProportion(t, g), i.drawCanvasImage(c, {
                            left: 0,
                            top: 0,
                            width: g.width / i.data.step,
                            height: g.height / i.data.step,
                            img: g.img
                        }), o.next = 18;
                        break;

                      case 17:
                        "color" == s.background.type && i.drawCanvasFillRect(c, {
                            width: t.width / i.data.step,
                            color: s.background.src,
                            height: t.height / i.data.step,
                            top: 0,
                            left: 0
                        });

                      case 18:
                        i.setData({
                            loading: 10
                        }), l = 0;

                      case 20:
                        if (!(l < s.poster_data.length)) {
                            o.next = 37;
                            break;
                        }
                        if ("logo" != (p = s.poster_data[l]).type && "img" != p.type && "other_img" != p.type && "head" != p.type && "qr" != p.type) {
                            o.next = 31;
                            break;
                        }
                        if (!p.src) {
                            o.next = 29;
                            break;
                        }
                        return o.next = 26, i.downImage(p);

                      case 26:
                        h = o.sent, p.img = h.img, i.drawCanvasImage(c, p);

                      case 29:
                        o.next = 32;
                        break;

                      case 31:
                        "title" == p.type || "nickname" == p.type ? (u = Math.floor(p.height / (p.size + 10)) || 1, 
                        p.line = u, i.drawCanvasLineText(c, p)) : "invite" == p.type || "text" == p.type || "mid" == p.type ? i.drawCanvasText(c, p) : "price" == p.type ? (p.src = d + ":￥" + p.src, 
                        i.drawCanvasText(c, p)) : "member_price" == p.type ? (p.src = "会员价:￥" + p.src, i.drawCanvasText(c, p)) : "market_price" == p.type && 0 != p.src && "0.00" != p.src && (p.src = n + ":￥" + p.src, 
                        i.drawCanvasText(c, p));

                      case 32:
                        w = parseInt(75 / s.poster_data.length * (l + 1)), i.setData({
                            loading: w + 10
                        });

                      case 34:
                        l++, o.next = 20;
                        break;

                      case 37:
                        f = setInterval(function() {
                            var t = i.data.loading + 2;
                            t = t >= 100 ? 100 : t, i.setData({
                                loading: t
                            }), t >= 100 && clearInterval(f);
                        }, 2e3), c.draw(!1, setTimeout(r(a.default.mark(function r() {
                            var s, o;
                            return a.default.wrap(function(a) {
                                for (;;) switch (a.prev = a.next) {
                                  case 0:
                                    return a.next = 2, i.toimage("myCanvas", t);

                                  case 2:
                                    s = a.sent, clearInterval(f), o = "listData[" + i.data.goodsId + "]", i.setData(e({
                                        loading: 100,
                                        poster: s
                                    }, o, s)), i.removeUnsafeList();

                                  case 7:
                                  case "end":
                                    return a.stop();
                                }
                            }, r);
                        })), 300));

                      case 39:
                      case "end":
                        return o.stop();
                    }
                }, o);
            }))();
        },
        startOldDrawCanvas: function(t, s) {
            var i = this;
            return r(a.default.mark(function o() {
                var n, d, c, g, l, p, h, u, w, f;
                return a.default.wrap(function(o) {
                    for (;;) switch (o.prev = o.next) {
                      case 0:
                        n = "原价";
                        try {
                            wx.getStorage({
                                key: "yz_basic_info",
                                success: function(t) {
                                    "" != t.data.lang.goods.market_price && (n = t.data.lang.goods.market_price);
                                }
                            });
                        } catch (t) {
                            console.log(t);
                        }
                        return (d = wx.createCanvasContext("myCanvas", i)).save(), i.drawRoundRectPath(d, t.width, t.height, 30), 
                        d.setFillStyle("#fff"), d.fillRect(0, 0, t.width, t.height), o.next = 9, i.downImage({
                            src: s.backgroundImg
                        });

                      case 9:
                        if (c = o.sent, i.setProportion(t, c), i.drawCanvasImage(d, {
                            left: 0,
                            top: 0,
                            width: c.width / i.data.step,
                            height: c.height / i.data.step,
                            img: c.img
                        }), i.setData({
                            loading: 10
                        }), g = {
                            actualWidth: 0,
                            actualText: ""
                        }, s.shopName && (g = i.measureTextWidth(d, {
                            width: 350,
                            size: 30,
                            text: s.shopName
                        }), i.setData({
                            loading: 18
                        }), i.drawCanvasLineText(d, {
                            top: 45,
                            left: 145 + (480 - g.actualWidth) / 2,
                            src: g.actualText,
                            size: 30,
                            color: "#333333",
                            width: 350,
                            line: 1
                        }), i.setData({
                            loading: 30
                        })), !s.logo) {
                            o.next = 20;
                            break;
                        }
                        return o.next = 18, i.downImage({
                            src: s.logo,
                            width: 70,
                            height: 70
                        });

                      case 18:
                        l = o.sent, i.drawCanvasCircular(d, {
                            img: l.img,
                            top: 25,
                            left: 55 + (480 - g.actualWidth) / 2,
                            width: 70,
                            height: 70
                        });

                      case 20:
                        if (i.setData({
                            loading: 40
                        }), !s.thumb) {
                            o.next = 26;
                            break;
                        }
                        return o.next = 24, i.downImage({
                            src: s.thumb,
                            width: 520,
                            height: 500
                        });

                      case 24:
                        p = o.sent, i.drawCanvasImage(d, {
                            img: p.img,
                            top: 120,
                            left: 60,
                            width: 520,
                            height: 500
                        });

                      case 26:
                        if (i.setData({
                            loading: 50
                        }), h = {
                            actualWidth: 0,
                            actualText: ""
                        }, s.price && (h = i.measureTextWidth(d, {
                            width: 400,
                            size: 30,
                            text: "￥" + s.price
                        }), i.drawCanvasLineText(d, {
                            top: 640,
                            left: 70,
                            src: "￥" + s.price,
                            size: 30,
                            color: "#f15353",
                            width: 400,
                            line: 1
                        })), i.setData({
                            loading: 55
                        }), s.market_price && 0 != s.market_price && "0.00" != s.market_price && (i.drawCanvasLineText(d, {
                            top: 645,
                            left: 100 + h.actualWidth,
                            src: n + ":￥" + s.market_price,
                            size: 22,
                            color: "#666666",
                            width: 200,
                            line: 1
                        }), u = i.measureTextWidth(d, {
                            width: 200,
                            size: 22,
                            text: n + ":￥" + s.market_price
                        }), i.drawCanvasFillRect(d, {
                            width: u.actualWidth,
                            color: "#999999",
                            height: 3,
                            top: 656,
                            left: 100 + h.actualWidth
                        })), i.setData({
                            loading: 60
                        }), s.shareTitle && i.drawCanvasLineText(d, {
                            top: 690,
                            left: 70,
                            src: s.shareTitle,
                            size: 26,
                            color: "#333333",
                            width: 400,
                            line: 2
                        }), i.setData({
                            loading: 65
                        }), !s.qrcode) {
                            o.next = 39;
                            break;
                        }
                        return o.next = 37, i.downImage({
                            src: s.qrcode,
                            height: 170,
                            width: 170
                        });

                      case 37:
                        w = o.sent, i.drawCanvasImage(d, {
                            img: w.img,
                            top: 800,
                            left: 400,
                            width: 170,
                            height: 170
                        });

                      case 39:
                        i.setData({
                            loading: 75
                        }), f = setInterval(function() {
                            var t = i.data.loading + 2;
                            t = t >= 100 ? 100 : t, i.setData({
                                loading: t
                            }), t >= 100 && clearInterval(f);
                        }, 2e3), d.draw(!1, setTimeout(r(a.default.mark(function r() {
                            var s, o;
                            return a.default.wrap(function(a) {
                                for (;;) switch (a.prev = a.next) {
                                  case 0:
                                    return a.next = 2, i.toimage("myCanvas", t);

                                  case 2:
                                    s = a.sent, clearInterval(f), o = "listData[" + i.data.goodsId + "]", i.setData(e({
                                        loading: 100,
                                        poster: s
                                    }, o, s)), i.removeUnsafeList();

                                  case 7:
                                  case "end":
                                    return a.stop();
                                }
                            }, r);
                        })), 300));

                      case 42:
                      case "end":
                        return o.stop();
                    }
                }, o);
            }))();
        }
    }
});